home *** CD-ROM | disk | FTP | other *** search
/ A Teacher's Guide to the Holocaust / A Teacher's Guide to the Holocaust.iso / data / people / scripts / elemdragclass.js < prev    next >
Text File  |  1999-12-05  |  10KB  |  359 lines

  1. // Copyright 1998,1999 Macromedia, Inc. All rights reserved.
  2.  
  3. //Constructs a drag and drop element
  4. function MM_drag(theParent, theName, theInitialValue,
  5.                 theIsDraggable, theIsTarget,
  6.                 theSnapsOnMiss, theSnapsOnIncorrect) {
  7.   // properties
  8.   this.initialValue = theInitialValue;
  9.   this.value = '';
  10.   this.disabled = true;
  11.   
  12.   this.snapsOnMiss = theSnapsOnMiss;
  13.   this.snapsOnIncorrect = theSnapsOnIncorrect;
  14.   this.origPosX = '';
  15.   this.origPosY = '';
  16.   
  17.   this._parent = theParent;
  18.   this._name = theName;
  19.   this._obj = '';
  20.   this._self = theParent._self+".e['"+theName+"']";
  21.   this._singleChoice = true;
  22.  
  23.   this._isDraggable = theIsDraggable;
  24.   this._isTarget = theIsTarget;
  25.   
  26.   this._posX = '';
  27.   this._posY = '';
  28.   this._prevPosX = '';
  29.   this._prevPosY = '';
  30.   this._width = '';
  31.   this._height = '';
  32.  
  33.   this.c = new Array();
  34.   
  35.   // member functions
  36.   this.init = MM_dragInit;
  37.   this.reset = MM_dragReset;
  38.   this.enable = MM_dragEnable;
  39.   this.disable = MM_dragDisable;
  40.   this.setDisabled = MM_dragSetDisabled;
  41.   this.update = MM_dragUpdate;
  42.   this.getPosition = MM_dragGetPosition;
  43.   this.snapTo = MM_dragSnapTo;
  44.   this.snapBack = MM_dragSnapBack;
  45.   this.moveToFront = MM_dragMoveToFront;
  46.   this.updateValue = MM_dragUpdateValue;
  47.   this.updateSources = MM_dragUpdateSources;
  48.   this.changeValue = MM_dragChangeValue;
  49.   this.setValue = MM_dragSetValue;
  50. }
  51.  
  52. //Called to initialize the drag objects
  53. function MM_dragInit() {
  54.   with (this) {
  55.     _obj = MM_intFindObject(_parent._self + _name);
  56.     if (_obj) {
  57.       _width =  parseInt((_parent.browserIsNS)?_obj.clip.width:_obj.offsetWidth);
  58.       _height = parseInt((_parent.browserIsNS)?_obj.clip.height:_obj.offsetHeight);
  59.       getPosition();
  60.       origPosX = _posX; origPosY = _posY;
  61.       if (_isDraggable)
  62.         MM_dragLayer(_parent._self + _name,'',0,0,0,0,true,false,-1,-1,-1,-1,false,false,0,_self+'.update()',true,_self+'.update(true)');
  63.   } }
  64. }
  65.  
  66. //Resets the element and its choices
  67. function MM_dragReset() {
  68.   var i;
  69.   if (this._obj) with (this) {
  70.     _parent.disabled ? disable() : enable();
  71.     value = initialValue;
  72.     if (_isDraggable) {
  73.       if (!changeValue(value)) {
  74.         snapTo(origPosX, origPosY);
  75.         updateValue(true);
  76.         updateSources();
  77.   } } }
  78. }
  79.  
  80. //Enables the element
  81. function MM_dragEnable() {
  82.   var i;
  83.   if (this._obj) with (this) {
  84.     disabled = false;
  85.     for (i in c)  if (i != 'length') c[i].disabled = false;
  86.     updateValue(true);
  87.     if (_isDraggable) _obj.MM_dragOk = true;
  88.   }
  89. }
  90.  
  91. //Disables the element
  92. function MM_dragDisable() {
  93.   with (this) {
  94.     disabled = true;
  95.     if (_isDraggable && _obj) _obj.MM_dragOk = null;
  96.   }
  97. }
  98.  
  99. //Calls the approppriate disable or enable function
  100. function MM_dragSetDisabled(theDisabled) {
  101.   if (theDisabled) this.disable();
  102.   else this.enable();
  103. }
  104.  
  105. //Called by MM_dragLayer when a drag object is dropped
  106. // Do a hit test on each of the targets
  107. function MM_dragUpdate(dragging) {
  108.   var i,noJudge=false,origValue,x,y;
  109.   with (this) {
  110.     if (dragging) {
  111.       if (this.onDrag != null) {
  112.         x = parseInt((_parent.browserIsNS)?_obj.pageX:_obj.style.pixelLeft);
  113.         y = parseInt((_parent.browserIsNS)?_obj.pageY:_obj.style.pixelTop);
  114.         onDrag(_parent._self+_name, x, y);
  115.       }
  116.     } else {
  117.       if (!_obj || disabled || !_isDraggable) return;
  118.  
  119.       origValue=value;
  120.       getPosition();
  121.       updateValue();
  122.  
  123.       if (snapsOnIncorrect && value && c[value].isCorrect == false) {
  124.         c[value].selected = false; // turn off selected flag
  125.         noJudge=true;
  126.       } else if (snapsOnMiss && !value) {
  127.         noJudge=true;
  128.       }
  129.       if (noJudge) {
  130.         snapBack();
  131.         if (origValue) {
  132.           c[origValue].selected = true;
  133.           value = origValue;
  134.         }
  135.         if (this.onDrag != null) onDrag(_parent._self+_name, _posX, _posY);
  136.         if (this.onDrop != null) onDrop(_parent._self+_name, value);
  137.       }
  138.  
  139.       if (!noJudge) updateSources();
  140.       
  141.       // call the parent's update
  142.       _parent.update(noJudge);
  143.   } }
  144. }
  145.  
  146. //Gets the current position of the layer
  147. function MM_dragGetPosition() {
  148.   if (this._obj) with (this) {
  149.     _prevPosX = _posX; _prevPosY = _posY;
  150.     _posX = parseInt((_parent.browserIsNS)?_obj.pageX:_obj.style.pixelLeft);
  151.     _posY = parseInt((_parent.browserIsNS)?_obj.pageY:_obj.style.pixelTop);
  152.   }
  153. }
  154.  
  155. //Moves the layer to the given position
  156. function MM_dragSnapTo(newPosX, newPosY) {
  157.   if (this._obj && newPosX && newPosY) with (this) {
  158.     if (_parent.browserIsNS) {
  159.       _obj.pageX = newPosX; _obj.pageY = newPosY;
  160.     } else {
  161.       _obj.style.pixelLeft = newPosX; _obj.style.pixelTop = newPosY;
  162.     }
  163.     _posX = newPosX; _posY = newPosY;
  164.   }
  165. }
  166.  
  167. //Moves the layer to its previous position
  168. function MM_dragSnapBack() {
  169.   with (this) snapTo(_prevPosX, _prevPosY);
  170. }
  171.  
  172. function MM_dragMoveToFront() {
  173.   var maxZ=0, i, aLayer, aLayerZ;
  174.   with (this) {
  175.     if (document.allLayers != null) {
  176.       for (i=0; i<document.allLayers.length; i++) {
  177.         aLayer = document.allLayers[i];
  178.         aLayerZ = (_parent.browserIsNS)?aLayer.zIndex:aLayer.style.zIndex;
  179.         if (aLayerZ > maxZ) maxZ = aLayerZ;
  180.       }
  181.       eval('_obj.'+((_parent.browserIsNS)?'':'style.')+'zIndex=maxZ+1');
  182.   } }
  183. }
  184.  
  185. //Update the value parameter and the choice selected flags
  186. function MM_dragUpdateValue(checkOnly) {
  187.   var i;
  188.   with (this) {
  189.     value = '';
  190.     for (i in c) if (i != 'length') 
  191.       if (value == '')  value = c[i].validValue(checkOnly);
  192.       else  c[i].validValue(checkOnly);
  193.   }
  194. }
  195.  
  196. //Update the sources for which we are a target
  197. function MM_dragUpdateSources() {
  198.   var i;
  199.   with (this) {
  200.     if (_isTarget) {
  201.       for (i in _parent.e)  if (i != 'length') {
  202.         if (_parent.e[i].value == _name) {
  203.           if (value != i) {  // update elem and choice
  204.             _parent.e[i].value = _parent.e[i].c[_name].validValue(true); // check only
  205.           } else {  // dropped on the element for which we were the target
  206.             _parent.e[i].value = '';
  207.             _parent.e[i].c[_name].selected = false;
  208.     } } } }
  209.     if (this.onDrag != null) onDrag(_parent._self+_name, _posX, _posY);
  210.     if (this.onDrop != null) onDrop(_parent._self+_name, value);
  211.   }
  212. }
  213.  
  214. //Moves the layer to the named target
  215. function MM_dragChangeValue(theValue) {
  216.   var retVal = false;
  217.   var choice, i;
  218.   if (this._obj && theValue && this._isDraggable) with (this) {
  219.     for (choice in c) if (choice != 'length' && choice == theValue) break;
  220.     if (choice == theValue) {
  221.       c[choice].setHotSpot();
  222.       snapTo(c[choice]._hitX, c[choice]._hitY);
  223.       moveToFront();
  224.       updateValue();  // set our selected state
  225.       updateSources();
  226.       retVal = true;
  227.   } }
  228.   return retVal;
  229. }
  230.  
  231. //Moves the layer to the named target, then updates the int
  232. function MM_dragSetValue(theValue) {
  233.   with (this) {
  234.     if (changeValue(theValue))
  235.       _parent.update(true); // update int, but don't judge
  236.   }
  237. }
  238.  
  239.  
  240. ////////////////////////////////////////
  241. //Create a drag and drop choice object
  242. function MM_dragTarg(theParent, theElement,
  243.                      theTarget, theIsCorrect, theScore,
  244.                      theTolerance, theAlignment, theOffset, theSnapsTo) {
  245.   // properties
  246.   this.expectedValue = theTarget;
  247.   this.isCorrect = theIsCorrect;
  248.   this.score = theScore;
  249.   this.selected = false;
  250.   this.disabled = false;
  251.   
  252.   this.tolerance = theTolerance;
  253.   this.alignment = theAlignment;
  254.   this.snapsTo = theSnapsTo;
  255.   this.offsetX = 0;
  256.   this.offsetY = 0
  257.   
  258.   this._elem = eval(theParent._self+".e['"+theElement+"']");
  259.   this._target = eval(theParent._self+".e['"+theTarget+"']");
  260.   this._isChoice = true;
  261.  
  262.   this._hitX = 0;
  263.   this._hitY = 0;
  264.   this._snapX = '';
  265.   this._snapY = '';
  266.   
  267.   if (theOffset != null) {
  268.     var  colonPos = theOffset.indexOf(":");
  269.     if (colonPos != -1) { //if colon separated, split strings
  270.       this.offsetX = parseInt(theOffset.substring(0,colonPos));
  271.       this.offsetY = parseInt(theOffset.substring(colonPos+1,theOffset.length));
  272.   } }
  273.   
  274.   // methods
  275.   this.validValue = MM_dragTargValidValue;
  276.   this.setHotSpot = MM_dragTargSetHotSpot;
  277.   this.setSelected = MM_dragTargSetSelected;
  278.   this.setDisabled = MM_dragTargSetDisabled;
  279. }
  280.  
  281. function MM_dragTargValidValue(checkOnly) {
  282.   var retVal = '';
  283.   with (this) {
  284.     selected = false;
  285.     if (!disabled) {
  286.     
  287.       setHotSpot();
  288.  
  289.       // check if the value is within the tolerance of the hotspot
  290.       if ((Math.pow(_hitX-_elem._posX,2) + Math.pow(_hitY-_elem._posY,2))
  291.               <= Math.pow(tolerance,2)) {
  292.         selected = true;
  293.         if (snapsTo && !checkOnly) _elem.snapTo(_snapX, _snapY);
  294.         retVal = expectedValue;
  295.   } } }
  296.   return retVal;
  297. }
  298.  
  299. function MM_dragTargSetHotSpot() {
  300.   var centerX, centerY, alignX=0, alignY=0;
  301.   with (this) {
  302.     // compute the center
  303.     centerX = Math.round(0.5 * (_target._width-_elem._width));
  304.     centerY = Math.round(0.5 * (_target._height-_elem._height));    
  305.     
  306.     // compute the alignment
  307.     if (alignment != '') {
  308.       // set to 'Center'
  309.       alignX = centerX; alignY = centerY;
  310.       if (alignment == 'Bottom')     alignY = _target._height;
  311.       else if (alignment == 'Top')   alignY = -_elem._height;
  312.       else if (alignment == 'Left')  alignX = -_elem._width;
  313.       else if (alignment == 'Right') alignX = _target._width;
  314.     }
  315.   
  316.     // update the targets position (needed for draggable targets)
  317.     _target.getPosition();
  318.   
  319.     // set the snap location
  320.     _snapX = _target._posX + alignX + offsetX;
  321.     _snapY = _target._posY + alignY + offsetY;
  322.     
  323.     // set the hit location
  324.     _hitX = _target._posX + centerX;
  325.     _hitY = _target._posY + centerY;
  326.     
  327.   }
  328. }
  329.  
  330. function MM_dragTargSetSelected(theSelected) {
  331.   with (this) {
  332.     if (theSelected) {
  333.       _elem.setValue(expectedValue);
  334.     } else {
  335.       selected = false;
  336.       if (_elem.value == expectedValue) _elem.value = '';
  337.       _elem._parent.update(true);
  338.     }
  339.   }
  340. }
  341.  
  342. function MM_dragTargSetDisabled(theDisabled) {
  343.   with (this) {
  344.     disabled = theDisabled;
  345.     if (disabled) {
  346.       selected = false;
  347.       if (_elem.value == expectedValue) _elem.value = '';
  348.     } else {
  349.       // NOTE: the selected state won't be correct 
  350.       //  if the snap location is outside of the tolerance
  351.       if (_elem._obj) {
  352.         validValue(true);
  353.         if (selected) _elem.value = expectedValue;
  354.       }
  355.     }
  356.     _elem._parent.update(true);
  357.   }
  358. }
  359.